package com.workfeng.message.config.quartz;

import com.workfeng.message.config.util.DateUtil;
import com.workfeng.message.config.webSocket.WebSocketServerEndpoint;
import org.jboss.logging.Logger;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * 自定义定时任务
 */
public class ScheduledJob implements Job {

    private static final Logger logger= Logger.getLogger(ScheduledJob.class);

    @Autowired
    private WebSocketServerEndpoint webSocketServerEndpoint;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        // 获取 jobDataMap
        JobDetail jobDetail = jobExecutionContext.getJobDetail();
        JobDataMap jobDataMap = jobDetail.getJobDataMap();
        // 获取消息businessKey,
        String businessKey = jobDataMap.getString("businessKey");
        // 再根据key获取消息内容
        String message = jobDataMap.getString(businessKey);

        //发送消息  执行任务逻辑....
        webSocketServerEndpoint.sendMessageToAll(message);

        logger.info("时间: "+ DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss")+" 执行定时任务: key: "+ businessKey +"，message: " + message);
    }
}
